package com.seeyon.apps.kkSample.utils.cap4;

import com.seeyon.apps.kkSample.utils.jdbc.JDAgentUtil;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.*;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.GlobalNames;
import com.seeyon.ctp.common.ModuleType;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.content.CtpContentAll;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.form.util.Enums;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.DBAgent;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.ctp.util.json.JSONUtil;
import com.seeyon.v3x.common.web.login.CurrentUser;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 表单数据工具类
 * @author
 * @date
 */
public class FormUtil {

    public static final EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
    public static final OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static final FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");

    private static final Log log = LogFactory.getLog(FormUtil.class);

    public static String getData(Object o) {
        if (null != o) {
            return String.valueOf(o);
        }
        return null;
    }

    public static String getData(Object o, Object define) {
        if (null != o) {
            return String.valueOf(o);
        } else {
            return String.valueOf(define);
        }
    }

    /**
     * FormUtil工具类
     * @param o    表单数据
     * @param type 要转换的数据格式
     * @return 转换后的数据
     */
    public static String getData(Object o, FieldType type) {
        return getData(o, type, "");
    }

    public static String getData(Object o, FieldType type, Object define) {
        boolean notNull = null != o && !String.valueOf(o).equals("");
        switch (type) {
            case text:
            case opinion:
                if (null != o) {
                    return String.valueOf(o);
                }
                return String.valueOf(define);
            case enumShowValue: {
                CtpEnumItem ctpEnumItem = null;
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        ctpEnumItem = enumManagerNew.getCacheEnumItem(id);
                        return ctpEnumItem.getShowvalue();
                    }
                } catch (Exception e) {
                    log.error("获取枚举名称异常" + e.getMessage(), e);
                }
                return String.valueOf(define);
            }
            case enumValue: {
                CtpEnumItem ctpEnumItem = null;
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        ctpEnumItem = enumManagerNew.getCacheEnumItem(id);
                        return ctpEnumItem.getValue();
                    }
                } catch (Exception e) {
                    log.error("获取枚举名称异常" + e.getMessage(), e);
                }
                return String.valueOf(define);
            }
            case enumShowCode: {
                CtpEnumItem ctpEnumItem = null;
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        ctpEnumItem = enumManagerNew.getCacheEnumItem(id);
                        return ctpEnumItem.getEnumItemCode();
                    }
                } catch (Exception e) {
                    log.error("获取枚举名称异常" + e.getMessage(), e);
                }
                return String.valueOf(define);
            }
            case deptName:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgDepartment department = orgManager.getDepartmentById(id);
                        return department.getName();
                    }
                } catch (BusinessException e) {
                    log.error("获取部门异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case deptCode:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgDepartment department = orgManager.getDepartmentById(id);
                        if (department.getCode() != null && !department.getCode().equals("")) {
                            return department.getCode();
                        }
                    }
                } catch (BusinessException e) {
                    log.error("获取部门异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case dateDay:
                if (null != o) {
                    Date date = (Date) o;// 申请日期
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    return df.format(date);
                }
                return String.valueOf(define);
            case dateDayTwo:
                if (null != o) {
                    Date date = (Date) o;// 申请日期
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                    return df.format(date);
                }
                return String.valueOf(define);
            case dateMinute:
                if (null != o) {
                    Date date = (Date) o;// 申请日期时间
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    return df.format(date);
                }
                return String.valueOf(define);
            case dateSecond:
                if (null != o) {
                    Date date = (Date) o;// 申请日期时间
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return df.format(date);
                }
                return String.valueOf(define);
            case dateTimestamp13:
                try {
                    if (null != o) {
                        Date date = (Date) o;// 申请日期时间
                        return String.valueOf(date.getTime());
                    }
                } catch (Exception e) {
                    log.error("获取时间异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case dateTimestamp10:
                try {
                    if (null != o) {
                        Date date = (Date) o;// 申请日期时间
                        String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                        Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
                        log.info("日期时间转日期：" + date2.getTime() / 1000);
                        return String.valueOf(date2.getTime() / 1000);
                    }
                } catch (Exception e) {
                    log.error("获取转日期时间戳10异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case memberName:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgMember member = orgManager.getMemberById(id);
                        return member.getName();
                    }
                } catch (BusinessException e) {
                    log.error("获取人员异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case memberCode:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgMember member = orgManager.getMemberById(id);
                        return member.getCode();
                    }
                } catch (BusinessException e) {
                    log.error("获取人员异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case memberLoginName:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgMember member = orgManager.getMemberById(id);
                        return member.getLoginName();
                    }
                } catch (BusinessException e) {
                    log.error("获取人员异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case deptCodeByMemberId:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgMember member = orgManager.getMemberById(id);
                        if (member != null) {
                            V3xOrgDepartment applicantDepartment = orgManager.getDepartmentById(member.getOrgDepartmentId());
                            if (applicantDepartment != null) {
                                return applicantDepartment.getCode();
                            }
                        }
                    }
                } catch (BusinessException e) {
                    log.error("获取人员异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
            case deptNameByMemberId:
                try {
                    if (notNull) {
                        Long id = Long.valueOf(String.valueOf(o));
                        V3xOrgMember member = orgManager.getMemberById(id);
                        if (member != null) {
                            V3xOrgDepartment applicantDepartment = orgManager.getDepartmentById(member.getOrgDepartmentId());
                            if (applicantDepartment != null) {
                                return applicantDepartment.getName();
                            }
                        }
                    }
                } catch (BusinessException e) {
                    log.error("获取人员异常：" + e.getMessage(), e);
                }
                return String.valueOf(define);
        }
        return String.valueOf(define);
    }

    /**
     * Cap4根据模板编号获取数据表名
     */
    public static String getTbNameByCode(String code) {
        try {
            FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
            FormBean cap4formBean = formApi4Cap4.getFormByFormCode(code);
            FormTableBean masterTableBeanRenYuan = cap4formBean.getMasterTableBean();
            return masterTableBeanRenYuan.getTableName();
        } catch (Exception e) {
            log.error("Cap4根据模板编号获取数据表名异常：" + e.getMessage(), e);
        }
        return null;
    }

    /**
     * Cap4根据模板编号获取数据表名
     * @param code    模板编号
     * @param display 明细表表名（明细表1）
     */
    public static String getTbSubNameByCode(String code, String display) {
        try {
            FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
            FormBean cap4formBean = formApi4Cap4.getFormByFormCode(code);
            List<FormTableBean> subTableBeanList = cap4formBean.getSubTableBean();
            for (FormTableBean formTableBean : subTableBeanList) {
                if (display.equals(formTableBean.getDisplay())) {
                    return formTableBean.getTableName();
                }
            }
            FormTableBean masterTableBeanRenYuan = cap4formBean.getMasterTableBean();
            return masterTableBeanRenYuan.getTableName();
        } catch (Exception e) {
            log.error("Cap4根据模板编号获取数据表名异常：" + e.getMessage(), e);
        }
        return null;
    }

    public static Map<String, String> getValueByDisplay(String code) {
        try {
            FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
            FormBean formBean = formApi4Cap4.getFormByFormCode(code);
            Map<String, String> map = new HashMap<>();
            formBean.getAllFieldBeans();
            List<FormTableBean> list = formBean.getTableList();
            formBean.getFormViewList();
            for (FormTableBean bean : list) {
                List<FormFieldBean> listBean = bean.getFields();
                for (FormFieldBean fieldBean : listBean) {
                    if (!"".equals(fieldBean.getDisplay()) && !"".equals(fieldBean.getDisplay())) {
                        map.put(fieldBean.getDisplay(), fieldBean.getName());
                    }
                }
            }
            return map;
        } catch (Exception e) {
            log.info("Cap4根据模板编号获取数据域名对应信息异常：" + e.getMessage(), e);
        }
        return null;
    }


    private static String jsonToXml(Map<String, Object> jsonMap) {
        String result = "";
        List<String> keyList = (List<String>) jsonMap.get("key");
        if (keyList == null) return result;
        StringBuilder xmlStrB = new StringBuilder();
        xmlStrB.append("<forms version=\"2.1\"><formExport><summary/><values>");
        for (String key : keyList) {
            xmlStrB.append("<column name=\"").append(key).append("\"><value><![CDATA[")
                    .append(jsonMap.get(key)).append("]]></value></column>");
        }
        xmlStrB.append("</values>");
        int flg = 1;
        List<String> keyList1 = (List<String>) jsonMap.get("keyList_" + flg);

        if (keyList1 != null) {
            xmlStrB.append("<subForms><subForm><values>");
            while (keyList1 != null) {
                List<Map<String, Object>> subList = (List<Map<String, Object>>) jsonMap.get("list_" + flg);
                for (Map<String, Object> sub : subList) {
                    xmlStrB.append("<row>");
                    for (String subKey : keyList1) {
                        xmlStrB.append("<column name=\"").append(subKey).append("\"><value><![CDATA[")
                                .append(sub.get(subKey)).append("]]></value></column>");
                    }
                    xmlStrB.append("</row>");
                }
                flg++;
                keyList1 = (List<String>) jsonMap.get("keyList_" + flg);
            }
            xmlStrB.append("</values></subForm></subForms>");
        }
        xmlStrB.append("</formExport></forms>");

        return String.valueOf(xmlStrB);
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("key", Arrays.asList("文本1", "文本2"));
        map.put("keyList_1", Arrays.asList("文本3", "文本4"));
        map.put("keyList_2", Arrays.asList("文本5"));
        map.put("文本1", "测试");
        map.put("文本2", "测试");
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list2 = new ArrayList<>();
        Map<String, Object> list1M1 = new HashMap<>();
        Map<String, Object> list1M2 = new HashMap<>();
        Map<String, Object> list2M1 = new HashMap<>();
        list1M1.put("文本3", "1");
        list1M2.put("文本4", "2");
        list2M1.put("文本5", 1);
        list1M1.put("文本3", 4);
        list1.add(list1M1);
        list1.add(list1M2);
        list2.add(list2M1);
        map.put("list_1", list1);
        map.put("list_2", list2);
        System.out.println(jsonToXml(map));
        System.out.println(JSONUtil.toJSONString(map));
    }

    /**
     * 获取
     * @param data 主表数据
     * @param code 模板编号
     * @return
     * @throws BusinessException
     * @throws SQLException
     */
    public static long getFormDataId(Map<String, Object> data, String code)
            throws BusinessException {
        FormBean formBean = formApi4Cap4.getFormByFormCode(code);
        if (formBean == null) {
            log.info("根据表单编号编号：" + code + " 未查询到对应底表模板信息：");
            return -1;
        }
        Map<String, Object> conditions = new HashMap<String, Object>();
        FormTableBean masterTableBean = formBean.getMasterTableBean();
        for (String key : data.keySet()) {
            FormFieldBean fieldBean = masterTableBean.getFieldBeanByDisplay(key);
            if (fieldBean == null) {
                continue;
            }
            conditions.put(fieldBean.getName(), data.get(key));
        }
        long masterid = queryTransData(conditions, masterTableBean.getTableName());

        return masterid;
    }

    /**
     * 新增或修改底表数据
     * @param data     主表数据
     * @param code     模板编号
     * @param masterid 判断是否是新增
     * @param subList  明细表数据
     * @return
     * @throws BusinessException
     * @throws SQLException
     */
    public static long saveOrUpdateMasterAndSubForm(Map<String, Object> data, String code, long masterid
            , List<Map<String, Object>> subList, String loginName)
            throws BusinessException, SQLException {
        FormBean formBean = formApi4Cap4.getFormByFormCode(code);
        if (formBean == null) {
            log.info("根据表单编号编号：" + code + " 未查询到对应底表模板信息：");
            return -1;
        }
//        AppContext.putThreadContext(com.seeyon.ctp.common.GlobalNames.SESSION_CONTEXT_USERINFO_KEY, user);
        setCurrentUser(loginName);
        //处理主表数据
        Map<String, Object> mainDataMap = new LinkedHashMap<String, Object>();
        boolean flat = false;
        if (masterid == -1L) {
            masterid = UUIDLong.longUUID();
            flat = true;
        } else {
            FormDataMasterBean updateBean = formApi4Cap4.findDataById(masterid, formBean, null);//--这种方式获取到单表信息
            mainDataMap = updateBean.getRowData();//获取主表
        }

        FormTableBean masterTableBean = formBean.getMasterTableBean();
        //webservice接口BEAN
        for (String key : data.keySet()) {
            if (StringUtils.isNotBlank(key)) {
                FormFieldBean fieldBean = masterTableBean.getFieldBeanByDisplay(key);
                if (fieldBean == null) {
                    continue;
                }
                Object value = data.get(key);
                FormFieldComEnum inputTypeEnum = fieldBean.getInputTypeEnum();
                if (inputTypeEnum == FormFieldComEnum.SELECT) {
                    value = getEumId(code, key, String.valueOf(value));
                }
                mainDataMap.put(fieldBean.getName(), value);
            }
        }
        mainDataMap.put("id", masterid);

        FormDataMasterBean masterData = new FormDataMasterBean(mainDataMap, masterTableBean);
        masterData.setId(masterid);
        if (CollectionUtils.isNotEmpty(subList)) {
            List<FormTableBean> tableList = formBean.getSubTableBean();
            //循环该表单下所有重复表(子表)
            for (FormTableBean table : tableList) {
                String tableName = table.getTableName();
                // 循环所有的底表数据
                for (Map<String, Object> map : subList) {
                    Map<String, Object> subDataMap = null;
                    for (String key : map.keySet()) {
                        FormFieldBean fieldBean = table.getFieldBeanByDisplay(key);
                        if (fieldBean == null) {
                            continue;
                        }
                        if (StringUtils.isNotBlank(map.get(key) + "")) {
                            if (subDataMap == null) {
                                subDataMap = new LinkedHashMap<String, Object>();
                            }
                            Object value = map.get(key);
                            FormFieldComEnum inputTypeEnum = fieldBean.getInputTypeEnum();
                            if (inputTypeEnum == FormFieldComEnum.SELECT) {
                                value = getEumId(code, key, String.valueOf(value));
                            }
                            subDataMap.put(fieldBean.getName(), value);
                        }
                    }
                    if (subDataMap != null) {
                        subDataMap.put(Enums.SubTableField.formmain_id.getKey(), masterid);
                        FormDataSubBean subData = new FormDataSubBean(subDataMap, table, masterData);
                        masterData.addSubData(tableName, subData);
                    }
                }
            }
        }
        formApi4Cap4.calcAll(formBean, masterData, formBean.getNewFormAuthViewBeans().get(0), true, true, true, true);// 执行计算公式
        if (flat) {
            masterData.setState(1);
            formApi4Cap4.saveOrUpdateFormData(masterData, formBean.getId(), true);
            CtpContentAll ctpContentAll = new CtpContentAll();
            ctpContentAll.setIdIfNew();
            ctpContentAll.setModuleType(ModuleType.cap4UnflowForm.getKey());
            ctpContentAll.setModuleId(masterid);
            ctpContentAll.setContentType(20);
            ctpContentAll.setContentTemplateId(formBean.getId());
            ctpContentAll.setContentDataId(masterid);
            ctpContentAll.setModifyId(AppContext.currentUserId());
            ctpContentAll.setModifyDate(new Date());
            ctpContentAll.setCreateId(AppContext.currentUserId());
            ctpContentAll.setCreateDate(new Date());
            ctpContentAll.setSort(0);
            DBAgent.save(ctpContentAll);//保存数据
        } else {
            formApi4Cap4.updateData(masterid, masterData.getFormTable().getTableName(), masterData.getRowData());
            FormDataMasterBean dataMasterBean = formApi4Cap4.findDataById(masterid, formBean, null);
            //删除所有明细数据 然后重新挂上去
            Map<String, List<FormDataSubBean>> subTables = masterData.getSubTables();
            for (String subTable : subTables.keySet()) {
                List<FormDataSubBean> subData = dataMasterBean.getSubData(subTable);
                if (subData != null && !subData.isEmpty()) {
                    String tableName = subData.get(0).getFormTable().getTableName();
                    for (FormDataSubBean subDatum : subData) {
                        JDAgentUtil.updateMore("delete from " + tableName + " where id = ?"
                                , Collections.singletonList(subDatum.getId()));
                    }
                    List<FormDataSubBean> subData1 = masterData.getSubData(subTable);
                    for (FormDataSubBean formDataSubBean : subData1) {
                        FormTableBean formTable = formDataSubBean.getFormTable();
                        Map<String, Object> rowData = formDataSubBean.getRowData();
                        String insertSql = "insert into " + formTable.getTableName() + " (";
                        String placeholder = ") values (";
                        List<Object> param = new ArrayList<>();
                        for (String key : rowData.keySet()) {
                            if (key.equals("groupsort")) {
                                continue;
                            }
                            insertSql += key + ", ";
                            placeholder += "?, ";
                            param.add(rowData.get(key));
                        }
                        insertSql = insertSql.substring(0, insertSql.length() - 2);
                        placeholder = placeholder.substring(0, placeholder.length() - 2);
                        insertSql += placeholder + ")";
                        JDAgentUtil.updateMore(insertSql, param);
                    }
                }
            }
//                List<FormTableBean> subTableBean = formBean.getSubTableBean();
//                FormTableBean formTableBean = subTableBean.get(0);
//                List<FormFieldBean> fields = formTableBean.getFields();
//                masterData.getSubData();
//
//                List<FormDataSubBean> formDataSubBeans = subTables.get(subTable);
//                if (formDataSubBeans != null && !formDataSubBeans.isEmpty()) {
//                    FormDataSubBean formDataSubBean = formDataSubBeans.get(0);
//                    Map<String, Object> rowData = formDataSubBean.getRowData();
//                    String insertSql = "insert into " + formDataSubBean.getFormTable().getTableName() + " (";
//                    String placeholder = ") values (";
//                    for (String key : rowData.keySet()) {
//                        if (key.equals("groupsort")) {
//                            continue;
//                        }
//                        insertSql += key + ", ";
//                        placeholder += "?, ";
//                    }
//                    insertSql = insertSql.substring(0, insertSql.length() - 2);
//                    placeholder = placeholder.substring(0, placeholder.length() - 2);
//
//                    insertSql += placeholder + ")";
//                    for (FormDataSubBean dataSubBean : formDataSubBeans) {
//                        List<Object> param = new ArrayList<>();
//                        Map<String, Object> dataSubBeanRowData = dataSubBean.getRowData();
//                        for (String key : rowData.keySet()) {
//                            if (key.equals("groupsort")) {
//                                continue;
//                            }
//                            param.add(dataSubBeanRowData.get(key));
//                        }
//                        JDAgentUtil.updateMore(insertSql, param);
//                    }
////                }
//            }
//            formApi4Cap4.updateData(masterid, masterData.getFormTable().getTableName(), masterData.getRowData());
//            for (String tableName : subTables.keySet()) {
//                List<FormDataSubBean> formDataSubBeans = subTables.get(tableName);
//                for (FormDataSubBean formDataSubBean : formDataSubBeans) {
//                    formApi4Cap4.updateData(formDataSubBean.getId()
//                            , formDataSubBean.getFormTable().getTableName(), formDataSubBean.getRowData());
//                }
//            }
            // formApi4Cap4.saveOrUpdateFormData(masterData, formBean.getId(), true);
        }
        return masterid;
    }

    /**
     * 设置当前用户
     */
    private static V3xOrgMember setCurrentUser(String loginName) throws BusinessException {
        V3xOrgMember admin = orgManager.getMemberByLoginName(loginName);
        User currentUser = new User();
        currentUser.setId(admin.getId());
        currentUser.setName(admin.getName());
        currentUser.setAccountId(admin.getOrgAccountId());
        currentUser.setLoginAccount(admin.getOrgAccountId());
        currentUser.setDepartmentId(admin.getOrgDepartmentId());
        CurrentUser.set(currentUser);
        AppContext.putThreadContext(GlobalNames.SESSION_CONTEXT_USERINFO_KEY, currentUser);
        return admin;
    }

    /**
     * 获取枚举id
     * @param formCode         表单模板编号
     * @param fieldDisplayName 控件名称
     * @param fieldValue       显示值
     * @return -1失败
     */
    public static Long getEumId(String formCode, String fieldDisplayName, String fieldValue) {
        try {
            FormBean formBean = formApi4Cap4.getFormByFormCode(formCode);
            FormFieldBean fieldBeanEnable = formBean.getFieldBeanByDisplay(fieldDisplayName);
            List<CtpEnumItem> emumItems = enumManagerNew.getEmumItemByEmumId(fieldBeanEnable.getEnumId());
            for (CtpEnumItem item : emumItems) {
                if (fieldValue.equals(item.getShowvalue())) {
                    return item.getId();
                }
            }
        } catch (Exception e) {
            log.error("会议列表数据#" + e.getMessage(), e);
        }
        return null;
    }

    public static long queryTransData(Map<String, Object> conditions, String tableName) {
        StringBuilder sub = new StringBuilder();
        sub.append("select * from " + tableName + " ");
        int num = 0;
        for (String key : conditions.keySet()) {
            if (num == 0) {
                sub.append(" where " + key + " = '" + conditions.get(key) + "'");
            } else {
                sub.append(" and " + key + " = '" + conditions.get(key) + "'");
            }
            num++;
        }
        JDBCAgent jdbcAgent = new JDBCAgent(true, false);
        try {
            log.info("执行查询表单数据");
            List<Map<String, Object>> result = jdbcAgent.findNamedSql(sub.toString());
            if (CollectionUtils.isNotEmpty(result)) {
                Object id = result.get(0).get("id");
                log.info("执行查询表单数据:" + id);
                return Long.valueOf(id.toString());
            }
        } catch (Exception e) {
            log.info("执行查询表单数据sql异常：" + sub.toString() + "" + e.getMessage(), e);
        } finally {
            jdbcAgent.close();
        }

        return -1;
    }

    public enum FieldType {
        text,
        opinion,
        enumShowValue,
        enumShowCode,
        enumValue,
        deptName,
        deptCode,
        dateDay,
        dateDayTwo,
        dateSecond,
        dateMinute,
        dateTimestamp13,
        dateTimestamp10,
        memberName,
        memberCode,
        memberLoginName,
        deptCodeByMemberId,
        deptNameByMemberId
    }

}
